package com.atjava.nebula.core.util.bean;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtilsBean;
import org.apache.commons.beanutils.PropertyUtilsBean;
import org.springframework.web.util.HtmlUtils;

import com.atjava.nebula.core.exception.baseExp.SystemException;


/**动态存取Bean的属性值，并对属性名称自动按照Java Bean的标准进行转换，如：把数据库的字段名转换成相应Bean属性名
 * @author kyo
 *
 */
public class MyBeanUtils {
	static BeanUtilsBean beanUtils;
	static{
		ConvertUtilsBean convertUtils = new ConvertUtilsBean();   
		StringConverter stringConverter = new StringConverter();
		convertUtils.deregister(String.class);
		convertUtils.register(stringConverter,String.class);
		
		DateConverter dateConverter = new DateConverter();
		convertUtils.deregister(Date.class);
		convertUtils.register(dateConverter, Date.class);
	    //因为要注册converter,所以不能再使用BeanUtils的静态方法了，必须创建BeanUtilsBean实例    
		beanUtils = new BeanUtilsBean(convertUtils,new PropertyUtilsBean());		
	}
	
	public static String getPropertyEx(Object bean , String name) throws SystemException
	{
		return getPropertyEx(bean,name,false) ;		
	}
	public static String getPropertyEx(Object bean , String name , boolean escape) throws SystemException
	{
		//name=JavaBeansUtil.getCamelCaseString(name, false);
		//get
		//net.sf.cglib.beans.BeanCopier.
		String res = null;
		try {
			res = getProperty(bean, name);
			if(escape){
				res = HtmlUtils.htmlEscape(res);
			}
		} catch (Exception e) {
			throw new SystemException(e, "动态存取Bean的属性值时出错") ;
		}
		return res ;		
	}
	public static String getProperty(Object bean , String name) throws SystemException
	{	
		Object value = null;
		
		//value=ReflectUtil.getFieldValue(bean, name);
		try {
			//beanUtils.
			value=beanUtils.getProperty(bean, name);
		} catch (Exception e) {
			throw new SystemException(e, "动态存取Bean的属性值时出错") ;
		} 
		if(value ==null)
			return null;
		else{
				return value.toString();
		}
			
	}
	
	
	public static void setPropertyEx(Object bean , String name , Object value) throws SystemException {
		//name=JavaBeansUtil.getCamelCaseString(name, false);
		setProperty(bean, name, value);
		
	}	
	
	
	public static void setProperty(Object bean , String name , Object value) throws SystemException {
		
		try {
			beanUtils.setProperty(bean, name, value);
		} catch (Exception e) {
			throw new SystemException(e, "动态设置Bean的属性值时出错") ;
		}
	}	
	
	public static void setProperty(Object bean , String name , String value) throws SystemException {

		//ReflectUtil.setFieldValue(bean, name, value);		
		try {
			beanUtils.setProperty(bean, name, value);
		} catch (Exception e) {
			throw new SystemException(e, "动态设置Bean的属性值时出错") ;
		}
		
	}	
	
	/**
	 * javaBean对象Clone处理，这是深复制方法。（对象中的引用对象也会复制） 注意：不能进行串行化的对象一定要设成 transient
	 * 
	 * @param ob
	 *            old对象
	 * @return  新对象
	 */
	public static Object deepClone(Object ob) {
		ByteArrayOutputStream bo = new ByteArrayOutputStream();
		Object newOb = null;
		try {
			ObjectOutputStream oo = new ObjectOutputStream(bo);
			oo.writeObject(ob);
			ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
			ObjectInputStream oi = new ObjectInputStream(bi);
			newOb = (oi.readObject());
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return newOb;
	}
	
	public static void main(String[] args) {
		System.out.println(new Date());
		//MyBeanUtils.

	}
	

}
